<template>
  <div>
    <el-form ref="form" v-model="form" label-width="80px" inline>
      <el-form-item label="商品名称">
        <el-input
          v-model="form.searchProductName"
          style="width: 170px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.prevent="searchInfo()">搜索</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="pageInfo.list" border style="width: 100%">
      <el-table-column
        label="序号"
        width="50"
      >
      <template slot-scope="scope">
          {{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="商品名称"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="价格(元)"
        width="100"
      ></el-table-column>
      <el-table-column prop="stock" label="库存" width="80"></el-table-column>
      <el-table-column prop="fileName" label="商品图片" width="120">
        <template slot-scope="scope">
          <el-image
            :src="`http://192.168.1.146:8088/${scope.row.fileName}`"
            width="100%"
            style="height: 80px; object-fit: cover"
          />
        </template>
      </el-table-column>
   
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              size="small"
              @click="
                $router.push({
                  name: 'productEdit',
                  query: { id: scope.row.id },
                })
              "
              style="margin-right: 10px"
            >
              编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              @click="delProduct(scope.row.id)"
              >删除</el-button
            >
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="changePageSize"
        @current-change="changePageNum"
        :current-page="form.pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "productPage",
  data() {
    return {
      pageInfo: {},
      form: {
        searchProductName: "",
        startDate: "",
        endDate: "",
        pageNum: 1,
        pageSize: 5,
      },
    };
  },
  methods: {
    searchInfo() {
      this.$axios
        .get("api/product/page", {
          params: {
            searchProductName: this.form.searchProductName,
            startDate: this.form.startDate,
            endDate: this.form.endDate,
            pageNum: this.form.pageNum,
            pageSize: this.form.pageSize,
          },
        })
        .then((response) => {
          this.pageInfo = response.data.pageInfo;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    changePageNum(newPageNum) {
      this.form.pageNum = newPageNum;
      this.searchInfo();
    },
    changePageSize(newPageSize) {
      this.form.pageSize = newPageSize;
      this.searchInfo();
    },
    viewProduct(id) {
      this.$axios
        .get(`api/user/find/${id}`)
        .then((response) => {
          if (response.data.findStatus) {
            alert("查看成功");
          }
        })
        .catch((error) => {
          this.$message.error("查看用户失败");
          console.error(error);
        });
    },
    delProduct(id) {
      if (confirm("确认删除该商品吗？")) {
        this.$axios
          .get(`api/product/del/${id}`)
          .then((response) => {
            if (response.data.delStatus) {
              alert("删除成功");
              this.searchInfo();
            }
          })
          .catch((error) => {
            this.$message.error("删除失败");
            console.error(error);
          });
      }
    },
  },
  mounted() {
    this.searchInfo();
  },
};
</script>

<style></style>
